package com.fzk.blog.blog.service.impl;

import com.fzk.blog.bean.Comment;
import com.fzk.blog.bean.User;
import com.fzk.blog.bean.vo.CommentInfo;
import com.fzk.blog.blog.service.ICommentService;
import com.fzk.blog.exception.MyException;
import com.fzk.blog.mapper.BlogMapper;
import com.fzk.blog.mapper.CommentMapper;
import com.fzk.blog.mapper.UserMapper;
import com.fzk.blog.utils.MyDateTimeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fzk
 * @date 2021-08-23 17:39
 */
@Service
public class CommentService implements ICommentService {
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private BlogMapper blogMapper;

    @Override
    public CommentInfo getCommentInfoById(BigInteger id) {
        // 1.查info
        CommentInfo commentInfo = commentMapper.getCommentInfoById(id);
        if (commentInfo == null)
            throw new MyException(400, "参数错误，id不存在：" + id);
        // 2.查询出创建user
        User user = userMapper.selectById(commentInfo.getUserId());

        commentInfo.setCreateUser(user);
        return commentInfo;
    }

    @Override
    public Map<String, Object> getCommentInfoListCondition(Integer blogId, Integer userId, Integer pageNo, Integer pageSize) {
        Map<String,Object> returnMap=new HashMap<>();
        // 1.计算index
        Integer index=null;
        if(pageNo!=null){
            if(pageSize==null)
                throw new MyException(400,"参数错误,page_no"+pageNo+";page_size:"+pageSize);
            else
                index=(pageNo-1)*pageSize;
        }
        // 2.查询list
        List<CommentInfo> commentInfos = commentMapper.getCommentInfoListPage(blogId, userId, index, pageSize);

        // 3.给每个info加入user
        for(CommentInfo c:commentInfos){
            c.setCreateUser(userMapper.selectById(c.getUserId()));
        }

        // 4.计算total
        if(index!=null){
            int total=commentMapper.getCommentInfoTotal(blogId, userId, index, pageSize);
            returnMap.put("total",total);
        }

        // 5.结果处理
        returnMap.put("data",commentInfos);
        return returnMap;
    }

    @Override
    @Transactional//开事务
    public CommentInfo insertComment(Comment comment, int loginId) {
        // 1.完善数据
        comment.setCreateBy(loginId);
        comment.setEditBy(loginId);
        comment.setFlag(0);
        comment.setUserId(loginId);
        comment.setPublishDate(MyDateTimeUtil.getNowDate());

        // 2.检查博客id
        if(blogMapper.getCreateById(comment.getBlogId())==null)
            throw new MyException(400,"参数错误：博客id不存在："+comment.getBlogId());

        // 3.插入评论
        if(commentMapper.insert(comment)!=1)
            throw new MyException(500,"插入失败");

        // 4. 获取commentInfo
        CommentInfo commentInfo=commentMapper.getCommentInfoById(comment.getId());

        // 5.获取createUser
        commentInfo.setCreateUser(userMapper.selectById(loginId));
        return commentInfo;
    }
}
